/**
 * Copyright (C), 2015-2019, XXX有限公司
 * FileName: CustFilingX3Data
 * Author:   Administrator
 * Date:     2019/6/11 15:03
 * Description: 客户建档中间表数据
 * History:
 * <author>          <time>          <version>          <desc>
 * 作者姓名           修改时间           版本号              描述
 */
package com.sunlord.dto;

import oracle.sql.TIMESTAMP;

import java.sql.Date;
import java.util.*;

/**
 * 〈一句话功能简述〉<br> 
 * 〈客户建档中间表数据〉
 *
 * @author Administrator
 * @create 2019/6/11
 * @since 1.0.0
 */
public class CustFilingX3Data {
    private final static String TRUE_FLAG = "2";
    private final static List<String> CURMAP = Arrays.asList("CNY","HK","TWD");
    /**
     * 主表数据
     */
    private HashMap<String, Object> bpCustomerData;
    /**
     * 客户地址
     */
    private List<Map<String, Object>> bpAddressDataList;
    /**
     * 收货地址
     */
    private List<Map<String, Object>> bpDlvcustDataList;
    /**
     * 联系人地址
     */
    private List<Map<String, Object>> contactDataList;


    @SuppressWarnings("AlibabaConstantFieldShouldBeUpperCase")
    private final static String ZBP_CUSTOMER_TABLE_NAME = "ZBPCUSTOMER";
    private final static String ZBP_CUSTOMER_SAVE_COL = "{'YREGBAN_0':'YREGBAN,VARCHAR2','YBANKAC_0':'YBANKAC,VARCHAR2','CCE0_0':'CCE0,VARCHAR2','CCE1_0':'CCE1,VARCHAR2','UPDTICK_0':'UPDTICK,NUMBER','OBJECTID_0':'OBJECTID,VARCHAR2','IMPFLG_0':'IMPFLG,NUMBER','BPCSNCDAT_0':'BPCSNCDAT,DATE','YRECORDFLG_0':'YRECORDFLG,NUMBER','BCGCOD_0':'BCGCOD,VARCHAR2'," +
            "'BPCNUM_0':'BPCNUM,VARCHAR2','BPRNAM_0':'BPRNAM,VARCHAR2','YBPRNAMEN_0':'YBPRNAMEN,VARCHAR2','BPAPYR_0':'BPAPYR,VARCHAR2','BPAADD_0':'BPAADD,VARCHAR2','CUR_0':'CUR,VARCHAR2','VACBPR_0':'VACBPR,VARCHAR2','PRITYP_0':'PRITYP,NUMBER'," +
            "'PTE_0':'PTE,VARCHAR2','ACCCOD_0':'ACCCOD,VARCHAR2','TSCCOD_0_0':'TSCCOD_0,VARCHAR2','TSCCOD_1_0':'TSCCOD_1,VARCHAR2','TSCCOD_2_0':'TSCCOD_2,VARCHAR2','TSCCOD_3_0':'TSCCOD_3,VARCHAR2','TSCCOD_4_0':'TSCCOD_4,VARCHAR2','REP_0_0':'REP_0,VARCHAR2'," +
            "'REP_1_0':'REP_1,VARCHAR2','BPDADD_0':'BPDADD,VARCHAR2','DAYMON_0':'DAYMON,NUMBER','OSTCTL_0':'OSTCTL,NUMBER','OSTAUZ_0':'OSTAUZ,NUMBER','YACCCHKREQ_0':'YACCCHKREQ,VARCHAR2','YACTFUND_0':'YACTFUND,VARCHAR2','YBILLADD_0':'YBILLADD,VARCHAR2'," +
            "'YBILLBAN_0':'YBILLBAN,VARCHAR2','YBILLCPY_0':'YBILLCPY,VARCHAR2','YBILLRECEIVE_0':'YBILLRECEIVE,VARCHAR2','YBILLRECTEL_0':'YBILLRECTEL,VARCHAR2','YBILLREQ_0':'YBILLREQ,VARCHAR2','YBILLSENDADD_0':'YBILLSENDADD,VARCHAR2','YBILLTEL_0':'YBILLTEL,VARCHAR2'" +
            ",'YBPCBUSTYP_0':'YBPCBUSTYP,NUMBER','YBPCPAYT_0':'YBPCPAYT,NUMBER','YBPCTRUENAM_0':'YBPCTRUENAM,VARCHAR2','YBPCUSTNO_0':'YBPCUSTNO,VARCHAR2','YBPUSR_0':'YBPUSR,VARCHAR2','YBUSINESSADD_0':'YBUSINESSADD,VARCHAR2','YCNPJ_0':'YCNPJ,VARCHAR2'," +
            "'YCOMPANYGRP_0':'YCOMPANYGRP,VARCHAR2','YCOMPANYTYP_0':'YCOMPANYTYP,VARCHAR2','YDOCREQ_0':'YDOCREQ,VARCHAR2','YFCY_0':'YFCY,VARCHAR2','YINVOICEBNK_0':'YINVOICEBNK,VARCHAR2','YINVOICEEND_0':'YINVOICEEND,VARCHAR2','YINVSPCE_0':'YINVSPCE,NUMBER'," +
            "'YISDISCUSTPO_0':'YISDISCUSTPO,NUMBER','YISPCS_0':'YISPCS,NUMBER','YISPRTBPCITM_0':'YISPRTBPCITM,NUMBER','YISPRTCUSTPO_0':'YISPRTCUSTPO,NUMBER','YITMREQ_0':'YITMREQ,VARCHAR2','YLP_0':'YLP,VARCHAR2','YNXETYEAR_0':'YNXETYEAR,VARCHAR2','YPACKREQ_0':'YPACKREQ," +
            "VARCHAR2','YPDREQ_0':'YPDREQ,VARCHAR2','YPIO_0':'YPIO,NUMBER','YPREPAID_0':'YPREPAID,VARCHAR2','YPRTLBE_0':'YPRTLBE,NUMBER','YREGADD_0':'YREGADD,VARCHAR2','YREGENDD_0':'YREGENDD,DATE','YREGFUND_0':'YREGFUND,VARCHAR2','YREGSTARTD_0':'YREGSTARTD,DATE'," +
            "'YSWACCNT_0':'YSWACCNT,VARCHAR2','YSWACCTEL_0':'YSWACCTEL,VARCHAR2','YSWDAYMON_0':'YSWDAYMON,VARCHAR2','YSWPAYDAY_0':'YSWPAYDAY,VARCHAR2','YTRSFRATIO_0':'YTRSFRATIO,NUMBER','YVMI_0':'YVMI,NUMBER','CREDATTIM_0':'CREDATTIM,TIMESTAMP'," +
            "'UPDDATTIM_0':'UPDDATTIM,TIMESTAMP','AUUID_0':'AUUID,RAW','CREUSR_0':'CREUSR,VARCHAR2','UPDUSR_0':'UPDUSR,VARCHAR2','BPCSTA_0':'BPCSTA,VACHAR2','X3RTMES_0':'X3RTMES,VACHAR2'}";

    private final static String ZBP_ADDRESS_TABLE_NAME = "ZBPADDRESS";
    private final static String ZBP_ADDRESS_SAVE_COL = "{'UPDTICK_0':'UPDTICK,NUMBER','POBJECTID_0':'POBJECTID,VARCHAR2','OBJECTID_0':'OBJECTID,VARCHAR2','BPCNUM_0':'BPCNUM,VARCHAR2','BPAADD_0':'BPAADD,VARCHAR2','BPAADDLIG_0':'BPAADDLIG_0,VARCHAR2','BPAADDLIG_1':'BPAADDLIG_1,VARCHAR2'," +
            "'BPAADDLIG_2':'BPAADDLIG_2,VARCHAR2','BPAADDFLG_0':'BPAADDFLG,NUMBER','CREDATTIM_0':'CREDATTIM,TIMESTAMP','UPDDATTIM_0':'UPDDATTIM,TIMESTAMP','AUUID_0':'AUUID,RAW','CREUSR_0':'CREUSR,VARCHAR2','UPDUSR_0':'UPDUSR,VARCHAR2'}";

    private final static String BPDLVCUST_TABLE_NAME = "ZBPDLVCUST";
    private final static String BPDLVCUST_SAVE_COL = "{'UPDTICK_0':'UPDTICK,NUMBER','POBJECTID_0':'POBJECTID,VARCHAR2','OBJECTID_0':'OBJECTID,VARCHAR2','BPCNUM_0':'BPCNUM,VARCHAR2','BPAADD_0':'BPAADD,VARCHAR2','BPDNAM_0':'BPDNAM_0,VARCHAR2','BPDNAM_1':'BPDNAM_1,VARCHAR2'," +
            "'STOFCY_0':'STOFCY,VARCHAR2','BPDADDFLG_0':'BPDADDFLG,NUMBER','CREDATTIM_0':'CREDATTIM,TIMESTAMP','UPDDATTIM_0':'UPDDATTIM,TIMESTAMP','AUUID_0':'AUUID,RAW','CREUSR_0':'CREUSR,VARCHAR2','UPDUSR_0':'UPDUSR,VARCHAR2'}";

    private final static String CONTACT_TABLE_NAME = "ZCONTACT";
    private final static String CONTACT_SAVE_COL = "{'UPDTICK_0':'UPDTICK,NUMBER','POBJECTID_0':'POBJECTID,VARCHAR2','OBJECTID_0':'OBJECTID,VARCHAR2','BPCNUM_0':'BPCNUM,VARCHAR2','CCNCRM_0':'CCNCRM,VARCHAR2','CNTFNC_0':'CNTFNC,NUMBER','BPAADD_0':'BPAADD,VARCHAR2','YK3KCNTTEL_0':'YK3KCNTTEL,VARCHAR2'," +
            "'CNTTTL_0':'CNTTTL,NUMBER','CNTLNA_0':'CNTLNA,VARCHAR2','CNTFLG_0':'CNTFLG,NUMBER','CREDATTIM_0':'CREDATTIM,TIMESTAMP','UPDDATTIM_0':'UPDDATTIM,TIMESTAMP','AUUID_0':'AUUID,RAW','CREUSR_0':'CREUSR,VARCHAR2','UPDUSR_0':'UPDUSR,VARCHAR2','LINNUM_0':'PARENTINDEX,VARCHAR2'}";

    /**
     * 如果是空就返回默认值
     * @param o
     * @param def
     * @return
     */
    public Object isEmptyToDefualt(Object o, Object def){

        if (o ==null){
            return def;
        }else if((o instanceof  String) && o.toString().trim().length()==0){
            return def;
        }
        return o;
    }
    /**
     * 如果是空就返回默认值
     * @param o
     * @return
     */
    public Object isNot1or2ToDefualt1(Object o){

        o = isEmptyToDefualt(o,"1");

        if (!"1".equals(o) && !"2".equals(o)){
            o="1";
        }
        return o;
    }


    public CustFilingX3Data(Map<String, Object> dataMaster, List<Map<String, Object>> dataBpaddress, List<Map<String, Object>> dataContact) {

        if (dataMaster == null || dataMaster.isEmpty() ){
            return;
        }
        bpCustomerData = new HashMap<>(dataMaster.size());
        bpCustomerData.putAll(dataMaster);
        bpCustomerData.put("YRECORDFLG", isEmptyToDefualt(dataMaster.get("YRECORDFLG"),"2"));

        bpCustomerData.put("ACCCOD",isEmptyToDefualt(dataMaster.get("ACCCOD"),"1122"));
        bpCustomerData.put("OSTCTL",isEmptyToDefualt(dataMaster.get("OSTCTL"),"2"));

        bpCustomerData.put("BPCNUM",isEmptyToDefualt(dataMaster.get("BPCNUM")," "));

        bpCustomerData.put("BPCSTA",isEmptyToDefualt(dataMaster.get("BPCSTA"),"2"));
        bpCustomerData.put("YRECORDFLG",isEmptyToDefualt(dataMaster.get("YRECORDFLG"),"2"));
        //更新标记  1 需要更新,2已更新
        bpCustomerData.put("IMPFLG", 1);
        //交易评级判断 客户级别是公司级1或行业级2，传值100；否者传0
        Object ypio = dataMaster.get("YPIO");
        bpCustomerData.put("YPIO",  "2".equals(ypio) || "1".equals(ypio) ? 100:0);

        //默认字段
        defualtFeild(bpCustomerData);


        //经营和注册详细地址 逻辑
        //币别：CNY,HK,TWD  字符串拼接：省+城市+详细地址
        //外币：字符串拼接顺序:详细地址+城市+省（区）+国家
        //经营地址:详细地址 [YBUSINESSADD],城市 [YCITY],省份 [YPROVINCE],国家 [YCOUNRRY]
        //注册地址:详细地址 [YREGADD]，城市 [YCITYZC],省份 [YPROVINCEZC],国家 [YCOUNTRYZC]
        String cur = "CUR";
        if(CURMAP.contains(dataMaster.get(cur))){
            bpCustomerData.put("YBUSINESSADD", concatAdds(dataMaster, "YCOUNRRY_XS", "YPROVINCE_XS", "YCITY_XS", "YBUSINESSADD"));
            bpCustomerData.put("YREGADD", concatAdds(dataMaster, "YCOUNTRYZC_XS", "YPROVINCEZC_XS", "YCITYZC_XS", "YREGADD"));
        }else{
            bpCustomerData.put("YBUSINESSADD", concatAdds(dataMaster, "YBUSINESSADD", "YCITY_XS", "YPROVINCE_XS", "YCOUNRRY_XS"));
            bpCustomerData.put("YREGADD", concatAdds(dataMaster, "YREGADD", "YCITYZC_XS", "YPROVINCEZC_XS", "YCOUNTRYZC_XS"));
        }


        // 客户地址
        Map<String,Object> bpAddressData;
        //收获地址
        Map<String,Object> bpDlvcustData;
        //联系人
        Map<String,Object> contactData;

        bpAddressDataList = new ArrayList<>();
        bpDlvcustDataList = new ArrayList<>();
        if(!dataBpaddress.isEmpty()){
            for (int i = 0; i <dataBpaddress.size(); i++) {
                bpAddressData = new HashMap<>();
                bpDlvcustData = new HashMap<>();

                //客户地址
                bpAddressData.put("CREATEDTIME",dataMaster.get("CREATEDTIME"));
                bpAddressData.put("POBJECTID",dataBpaddress.get(i).get("PARENTOBJECTID"));
                bpAddressData.put("OBJECTID",dataBpaddress.get(i).get("OBJECTID"));
                bpAddressData.put("BPAADD",dataBpaddress.get(i).get("BPAADD"));
                //详细地址 拆分三段
                String bpdAdd = Optional.ofNullable(dataBpaddress.get(i).get("BPDADD")).orElse(" ").toString();
                bpAddressData.put("BPAADDLIG_0", bpdAdd.length()>80 ?  bpdAdd.substring(0,80):bpdAdd);
                bpAddressData.put("BPAADDLIG_1",bpdAdd.length()>80 ? bpdAdd.substring(80,(bpdAdd.length()>160 ? 160: bpdAdd.length())) : " ");
                bpAddressData.put("BPAADDLIG_2",bpdAdd.length()>160 ? bpdAdd.substring(160,(bpdAdd.length()>240 ? 240: bpdAdd.length())) : " ");

                bpAddressData.put("BPAADDFLG",isNot1or2ToDefualt1(dataBpaddress.get(i).get("BPAADDFLG")));

                bpAddressData.put("BPCNUM",dataMaster.get("BPCNUM"));

                //默认字段
                defualtFeild(bpAddressData);
                //主数据里地址填的编码
                if(TRUE_FLAG.equals(dataBpaddress.get(i).get("BPAADDFLG"))){
                    //地址
                    bpCustomerData.put("BPAPYR",bpAddressData.get("BPAADD"));
                    //默认地址
                    bpCustomerData.put("BPAADD",bpAddressData.get("BPAADD"));
                }
                //收货地址
                if(TRUE_FLAG.equals(dataBpaddress.get(i).get("YSHFLG"))){
                    bpDlvcustData.put("CREATEDTIME",dataMaster.get("CREATEDTIME"));
                    bpDlvcustData.put("POBJECTID",dataBpaddress.get(i).get("PARENTOBJECTID"));
                    bpDlvcustData.put("OBJECTID",dataBpaddress.get(i).get("OBJECTID"));
                    bpDlvcustData.put("BPAADD",dataBpaddress.get(i).get("BPAADD"));
                    bpDlvcustData.put("BPDNAM_0",dataMaster.get("BPRNAM"));
                    bpDlvcustData.put("BPDNAM_1",dataMaster.get("BPRNAM"));
                    bpDlvcustData.put("STOFCY",dataBpaddress.get(i).get("STOFCY"));
                    bpDlvcustData.put("BPDADDFLG",isNot1or2ToDefualt1(dataBpaddress.get(i).get("YDEFSHFLG")));
                    bpDlvcustData.put("BPCNUM",dataMaster.get("BPCNUM"));
                    //默认字段
                    defualtFeild(bpDlvcustData);
                    //默认收获地址
                    if(TRUE_FLAG.equals(dataBpaddress.get(i).get("YDEFSHFLG"))){
                        bpCustomerData.put("BPDADD",bpDlvcustData.get("BPAADD"));
                    }
                    bpDlvcustDataList.add(bpDlvcustData);
                }
                bpAddressDataList.add(bpAddressData);
            }
        }

        contactDataList = new ArrayList<>();
        if(!dataContact.isEmpty()){
            for (int i = 0; i < dataContact.size(); i++) {
                contactData = new HashMap<>();
                contactData.put("CREATEDTIME",dataMaster.get("CREATEDTIME"));
                contactData.put("POBJECTID",dataContact.get(i).get("PARENTOBJECTID"));
                contactData.put("OBJECTID",dataContact.get(i).get("OBJECTID"));
                contactData.put("BPCNUM",dataMaster.get("BPCNUM"));
                contactData.put("CNTFNC",dataContact.get(i).get("CNTFNC"));

                contactData.put("CCNCRM",dataContact.get(i).get("CCNCRM"));
                contactData.put("YSTOFCY",dataContact.get(i).get("YSTOFCY"));
                contactData.put("BPAADD",dataContact.get(i).get("BPAADD"));
                contactData.put("YK3KCNTTEL",dataContact.get(i).get("YK3KCNTTEL"));

                contactData.put("CNTTTL",dataContact.get(i).get("CNTTTL"));
                contactData.put("CNTLNA",dataContact.get(i).get("CNTLNA"));
                contactData.put("CNTFLG",isNot1or2ToDefualt1(dataContact.get(i).get("CNTFLG")));

                contactData.put("PARENTINDEX",dataContact.get(i).get("PARENTINDEX"));

                //默认字段
                defualtFeild(contactData);
                contactDataList.add(contactData);
            }
        }



    }

    private void defualtFeild(Map<String,Object> map) {
        String createdtime = "CREATEDTIME";
        if(!map.containsKey(createdtime)){
            map.put("CREDATTIM",new TIMESTAMP(new Date(System.currentTimeMillis())));
        }
        map.put("UPDDATTIM",new TIMESTAMP(new Date(System.currentTimeMillis())));
        map.put("AUUID", UUID.randomUUID().toString().replace("-",""));
        map.put("CREUSR"," ");
        map.put("UPDUSR"," ");
        map.put("UPDTICK","1");
    }

    private String concatAdds(Map<String, Object> dataMaster, String ycounrry, String yprovince, String ycity, String ybusinessadd) {
        return Optional.ofNullable(dataMaster.get(ycounrry)).orElse(" ").toString()
                .concat(Optional.ofNullable(dataMaster.get(yprovince)).orElse(" ").toString()
                        .concat(Optional.ofNullable(dataMaster.get(ycity)).orElse(" ").toString()
                                .concat(Optional.ofNullable(dataMaster.get(ybusinessadd)).orElse(" ").toString())));
    }


    public HashMap<String, Object> getBpCustomerData() {
        return bpCustomerData;
    }

    public void setBpCustomerData(HashMap<String, Object> bpCustomerData) {
        this.bpCustomerData = bpCustomerData;
    }

    public List<Map<String, Object>> getBpAddressDataList() {
        return bpAddressDataList;
    }

    public void setBpAddressDataList(List<Map<String, Object>> bpAddressDataList) {
        this.bpAddressDataList = bpAddressDataList;
    }

    public List<Map<String, Object>> getBpDlvcustDataList() {
        return bpDlvcustDataList;
    }

    public void setBpDlvcustDataList(List<Map<String, Object>> bpDlvcustDataList) {
        this.bpDlvcustDataList = bpDlvcustDataList;
    }

    public List<Map<String, Object>> getContactDataList() {
        return contactDataList;
    }

    public void setContactDataList(List<Map<String, Object>> contactDataList) {
        this.contactDataList = contactDataList;
    }

    public static String getZbpCustomerTableName() {
        return ZBP_CUSTOMER_TABLE_NAME;
    }

    public static String getZbpCustomerSaveCol() {
        return ZBP_CUSTOMER_SAVE_COL;
    }

    public static String getZbpAddressTableName() {
        return ZBP_ADDRESS_TABLE_NAME;
    }

    public static String getZbpAddressSaveCol() {
        return ZBP_ADDRESS_SAVE_COL;
    }

    public static String getBpdlvcustTableName() {
        return BPDLVCUST_TABLE_NAME;
    }

    public static String getBpdlvcustSaveCol() {
        return BPDLVCUST_SAVE_COL;
    }

    public static String getContactTableName() {
        return CONTACT_TABLE_NAME;
    }

    public static String getContactSaveCol() {
        return CONTACT_SAVE_COL;
    }






}